package com.powernode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.SysRole;
import com.powernode.model.Result;
import com.powernode.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author RenBoQing
 * @date 2024年07月09日 20:19
 * @Description
 */
@Api(tags = "系统角色接口管理")
@RequestMapping("sys/role")
@RestController
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;
    /*
     * 查询系统角色列表
     * @author RenBoQing
     * @date 2024/8/29 19:54
     * @return com.powernode.model.Result<java.util.List<com.powernode.domain.SysRole>>
     */
    @ApiOperation("查询系统角色列表")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sys:role:list')")
    public Result<List<SysRole>> loadSysRoleList() {
        List<SysRole> roles = sysRoleService.queryAllSysRoleList();
        return Result.success(roles);
    }
    /*
     * @author RenBoQing
     * @date 2024/7/12 21:15
     * @param current 当前页
     * @param size 每页的条数
     * @param roleName  角色的名称
     * @return com.powernode.model.Result<com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.powernode.domain.SysRole>>
     */
    @ApiOperation("多条件分页查询角色列表")
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('sys:role:page')")
    public Result<Page<SysRole>> loadSysRolePage(@RequestParam(defaultValue = "1") Long current,
                                                 @RequestParam(defaultValue = "10") Long size,
                                                 @RequestParam(required = false) String roleName) {
        Page<SysRole> page = new Page<>(current, size);
        page = sysRoleService.page(page, new LambdaQueryWrapper<SysRole>().like(StringUtils.hasText(roleName), SysRole::getRoleName, roleName).orderByDesc(SysRole::getCreateTime));
        return Result.success(page);
    }
    /*
     *
     * @author RenBoQing
     * @date 2024/7/12 21:49
     * @param sysRole
     * @return com.powernode.model.Result<java.lang.String>
     */
    @ApiOperation("新增角色")
    @PostMapping("")
    @PreAuthorize("hasAuthority('sys:role:save')")
    public Result<String> saveSysRole(@RequestBody SysRole sysRole) {
        boolean result = sysRoleService.saveSysRole(sysRole);
        return Result.handle(result);
    }
    /*
     * 根据角色的标识查询角色详情
     * @author RenBoQing
     * @date 2024/7/18 21:56
     * @param roleId
     * @return com.powernode.model.Result<com.powernode.domain.SysRole>
     */
    @ApiOperation("根据角色的标识查询角色详情")
    @GetMapping("/info/{roleId}")
    @PreAuthorize("hasAuthority('sys:role:info')")
    public Result<SysRole> loadSysRoleInfo(@PathVariable Long roleId) {
        SysRole sysRole = sysRoleService.querySysRoleInfoByUserId(roleId);
        return Result.success(sysRole);
    }
   /*
    * 修改角色信息
    * @author RenBoQing
    * @date 2024/7/18 22:19
    * @param sysRole
    * @return com.powernode.model.Result<java.lang.String>
    */
    @ApiOperation("修改角色信息")
    @PutMapping("")
    @PreAuthorize("hasAuthority('sys:role:update')")
    public Result<String> modifySysRole(@RequestBody SysRole sysRole) {
        boolean result = sysRoleService.modifySysRole(sysRole);
        return Result.handle(result);
    }
    /*
     * 删除单个角色/批量删除角色
     * @author RenBoQing
     * @date 2024/7/18 22:44
     * @param roleIds
     * @return com.powernode.model.Result<java.lang.String>
     */
    @ApiOperation("删除单个角色/批量删除角色")
    @DeleteMapping("")
    @PreAuthorize("hasAuthority('sys:role:delete')")
    public Result<String> removeSysRoleListByUserIds(@RequestBody List<Long> roleIdList) {
        boolean result = sysRoleService.removeSysUserListByUserIds(roleIdList);
        return Result.handle(result);
    }
}
